package com.yikaiye.android.yikaiye.ui.activity.login;

import android.text.TextUtils;

import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.application.MyApplication;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityBindPhoneNumBinding;
import com.yikaiye.android.yikaiye.ui.activity.MainActivity;
import com.yikaiye.android.yikaiye.ui.activity.login.viewmodel.BindPhoneViewModel;
import com.yikaiye.android.yikaiye.utils.JumpIntent;
import com.yikaiye.android.yikaiye.utils.ToastHelper;

/**
 * 绑定手机号
 */
public class BindPhoneNumActivity extends BaseActivity<ActivityBindPhoneNumBinding, BindPhoneViewModel> {

    private String userId;

    @Override
    protected BindPhoneViewModel bindModel() {
        return getViewModel(this, BindPhoneViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void initData() {
        if (getIntent().getExtras() != null) {
            userId = getIntent().getExtras().getString(Constant.USER_ID_KEY);
        }
        binding.setBindPhoneViewModel(viewModel);
    }

    @Override
    protected void initClick() {
        viewModel.onDelayClick(binding.back, o -> finish());

        viewModel.onDelayClick(binding.cdtCountdown, o -> {
            showLoadingDialog();
            viewModel.sendCode(5, viewModel.phoneNumber.getValue());
        });

        viewModel.onDelayClick(binding.btnConfirm, o -> {
            showLoadingDialog();
            viewModel.bindPhone(userId, viewModel.phoneNumber.getValue(), viewModel.code.getValue());
        });

        viewModel.phoneNumber.observe(this, s -> changeBtnStatus());
        viewModel.code.observe(this, s -> changeBtnStatus());

        // 获取验证码成功
        viewModel.codeSuccess.observe(this, verifyCodeModel -> {
            dismissLoadingDialog();
            binding.cdtCountdown.startCountDown(
                    context,
                    60,
                    getString(R.string.send_code_again),
                    "s"
            );
            ToastHelper.show(getString(R.string.code_has_been_sent));
        });
        // 获取验证码失败
        viewModel.codeError.observe(this, s -> {
            dismissLoadingDialog();
            ToastHelper.show(s);
        });

        // 绑定成功
        viewModel.bindSuccess.observe(this, userModel -> {
            if (userModel != null) {
                Constant.isLoginToken = false;
                if (!userModel.isForceLogout()) {
                    Constant.isJumpToLogin = true;
                    MyApplication.manager.setLogin(true);
                    MyApplication.manager.setPhone(viewModel.phoneNumber.getValue());
                    JumpIntent.jump(BindPhoneNumActivity.this, MainActivity.class);
                } else {
                    finish();
                    ToastHelper.show(getString(R.string.bind_success_login_again));
                }
                dismissLoadingDialog();
            }
        });
        // 绑定失败
        viewModel.bindError.observe(this, s -> {
            ToastHelper.show(s);
            dismissLoadingDialog();
        });
    }

    private void changeBtnStatus() {
        if (!TextUtils.isEmpty(viewModel.phoneNumber.getValue())
                && !TextUtils.isEmpty(viewModel.code.getValue())) {
            binding.btnConfirm.setEnabled(true);
            binding.btnConfirm.setBackgroundResource(R.drawable.shape_f93f50_4dp);
        } else {
            binding.btnConfirm.setEnabled(false);
            binding.btnConfirm.setBackgroundResource(R.drawable.shape_fcebea_4dp);
        }
    }
}